import wx
from Constant import *
'''
GameObj_Exit :

    Game object of exit, When it closes, it is a common cube. Once exit opens, it will start it's 
    
    animation.
'''
class GameObj_Exit():
    '''
    Constructor : GameObj_Exit
        mainMap - object pointer - MainMap
        pos - position of this object
    '''
    def __init__(self, mainMap, pos):
        self._001_initialInstanceVariable(mainMap, pos)
        self._002_bindAction()
    '''
    function _001 : initialize the variables used for this object
        mainMap - object pointer - MainMap
        pos - position of this object
    '''
    def _001_initialInstanceVariable(self, mainMap, pos):
        #object pointer - MainMap
        self.mainMap = mainMap
        #record current position in map (row, column)
        self.posInMap = pos
        #the index of current image
        self.curImageIndex = 0
        #timer of open exit
        self.timerOpenExit = wx.Timer()
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        #bind effect lasting of exit to timer
        self.timerOpenExit.Bind(wx.EVT_TIMER, self.SLOT001_exitOpenEffect)
        #bind destroy action to mainMap object
        self.mainMap.evtCollector.Bind(EVT_DESTROY_GAME_OBJ, self.SLOT002_destroy)
        #bind action of opening exit
        self.mainMap.evtCollector.Bind(EVT_OPEN_EXIT, self.SLOT003_openExit)
    '''
    function _003 : destroy all object of this class
    '''
    def _003_destroyAllObject(self):
        self.timerOpenExit.Stop()
        self.timerOpenExit.Destroy()
        del self
    '''
    function SLOT001 : the slot of lasting the open effect of exit 
    '''
    def SLOT001_exitOpenEffect(self, event):
        self.curImageIndex = (self.curImageIndex + 1) % LIST_FILENAME_GAMEOBJECT[GAME_OBJ_EXIT].__len__()
        self.mainMap.ACT003_changeObj(self.posInMap, GAME_OBJ_EXIT, self.curImageIndex)
    '''
    function SLOT002 : the slot of destroying
    '''
    def SLOT002_destroy(self, event):
        if not event.isClearAll :
            if event.delObjType == GAME_OBJ_EXIT and self.posInMap[0] == event.delPos[0] and \
            self.posInMap[1] == event.delPos[1] :
                self._003_destroyAllObject()
        else :
            self._003_destroyAllObject()
        event.Skip()
    '''
    function SLOT002 : the slot of opening the exit
    '''
    def SLOT003_openExit(self, event):
        if self.mainMap.GET001_objTypeIn_listCube(self.posInMap) == GAME_OBJ_EXIT :
            self.mainMap.SET001_isExitOpen(False)
            self.mainMap.SET001_isExitOpen(True)
            self.timerOpenExit.Start(1000)
        event.Skip()